﻿package baidu.reader.common.ui.components
{
    import baidu.reader.common.*;
    import baidu.ui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.text.*;
    import flash.ui.*;

    public class PageNumberInputPanel extends Sprite
    {
        private var jump_button:Button;
        private var inputBG:JumpInputBG;
        private var inputTextField:TextField;
        private var _previousPage:int;

        public function PageNumberInputPanel()
        {
            this.initUI();
            return;
        }// end function

        private function initUI() : void
        {
            this.inputBG = new JumpInputBG();
            addChild(this.inputBG);
            this.jump_button = new Button();
            this.jump_button.setStyle("skin", JumpToPageButtonSkin);
            this.jump_button.useHandCursor = true;
            this.jump_button.addEventListener(MouseEvent.CLICK, this.jumpButtonClickHandler);
            addChild(this.jump_button);
            this.jump_button.x = this.inputBG.width - 5;
            var _loc_1:* = new TextFormat("Microsoft Yahei,微软雅黑,黑体", 14, 5516307);
            _loc_1.bold = true;
            _loc_1.align = TextFormatAlign.RIGHT;
            this.inputTextField = new TextField();
            this.inputTextField.filters = [new BlurFilter(0, 0, 0)];
            this.inputTextField.defaultTextFormat = _loc_1;
            this.inputTextField.type = TextFieldType.INPUT;
            this.inputTextField.restrict = "0-9";
            this.inputTextField.width = this.inputBG.width - 10;
            this.inputTextField.height = this.inputBG.height - 4;
            this.inputTextField.x = this.inputBG.x + 2;
            this.inputTextField.y = this.inputBG.y + 2;
            this.inputTextField.addEventListener(KeyboardEvent.KEY_DOWN, this.keyDownHandler);
            addChild(this.inputTextField);
            return;
        }// end function

        private function keyDownHandler(event:KeyboardEvent) : void
        {
            if (event.keyCode == Keyboard.ENTER)
            {
                dispatchEvent(new ReaderEvent(ReaderEvent.GO_TO_POSITION, {docid:null, pn:this.inputTextField.text, offset:null}));
                stage.focus = null;
            }
            return;
        }// end function

        private function jumpButtonClickHandler(event:MouseEvent) : void
        {
            if (this._previousPage == int(this.inputTextField.text))
            {
                return;
            }
            dispatchEvent(new ReaderEvent(ReaderEvent.GO_TO_POSITION, {docid:null, pn:this.inputTextField.text, offset:null}));
            return;
        }// end function

        public function set pageNumber(param1:int) : void
        {
            this.inputTextField.text = String(param1);
            this._previousPage = param1;
            return;
        }// end function

        public function focus() : void
        {
            if (stage)
            {
                stage.focus = this.inputTextField;
            }
            this.inputTextField.setSelection(0, this.inputTextField.text.length);
            return;
        }// end function

    }
}
